package com.codeoss.framework.view.parser;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.data.metadata.Column;
import com.codeoss.framework.data.metadata.Table;
import com.codeoss.framework.data.store.ITableStore;
import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.ExpressionUtil;
import com.codeoss.framework.util.DictDef;
import com.codeoss.framework.util.LookupInfo;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.Tag;
import com.codeoss.framework.view.datasource.DataSourceConfig;
import com.codeoss.framework.view.util.DataSourceUtil;
import com.codeoss.module.ir.model.IrDict;
import com.codeoss.module.ir.model.IrDictLine;
import com.codeoss.module.ir.service.IIrDictLineService;
import com.codeoss.module.ir.service.IIrDictService;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

@Service
public class TagParser extends AbstractComponentParser {
	@Autowired
	private ITableStore tableStore;
	@Autowired
	private DataSourceUtil dataSourceUtil;
	@Autowired
	private IIrDictService irDictService;
	@Autowired
	private IIrDictLineService irDictLineService;
	protected void setAttributes(IParseContext parseContext, ObjectNode objectNode, Component component) {
		super.setAttributes(parseContext, objectNode, component);
		Tag tagComponent = (Tag) component;
		String labelExpression = objectNode.get("labelExpression").asText();
		JsonNode jsonNode=objectNode.get("col");
		if (!StringUtils.isEmpty(labelExpression)) {
			Expression expression = ExpressionUtil.convertLabelToExpression(labelExpression,Expression.EXPRESSION_TYPE_JAVASCRIPT);
			tagComponent.setLabelExpression(expression);
		}
		ObjectNode propertyMapNode=(ObjectNode)objectNode.get("propertyMap");
		if(propertyMapNode!=null) {
			jsonNode=propertyMapNode.get("dataSource");
			if(jsonNode!=null) {
				String dataSource=propertyMapNode.get("dataSource").asText();
				tagComponent.setDataSource(dataSource);
			}
			jsonNode=propertyMapNode.get("dataField");
			if(jsonNode!=null) {
				String dataField=propertyMapNode.get("dataField").asText();
				tagComponent.setDataField(dataField);
				
				DataSourceConfig dataSourceConfig=dataSourceUtil.getDataSourceConfigById(parseContext.getPage(), tagComponent.getDataSource());
				Table table= tableStore.get(dataSourceConfig.getTableName());
				
				jsonNode=propertyMapNode.get("varName");
				String varName="";
				if(jsonNode==null ||StringUtils.isEmpty(jsonNode.asText())) {
					if(!StringUtils.isEmpty(tagComponent.getDataSource()) && !StringUtils.isEmpty(tagComponent.getDataField())) {
						varName=table.getTableName()+"_"+tagComponent.getDataField();
					}
				}else {
					varName=propertyMapNode.get("varName").asText();
				}
				tagComponent.setVarName(varName);
			}
			
			
			jsonNode=propertyMapNode.get("colorIndicatorExpression");
			if(jsonNode!=null) {
				tagComponent.setColorIndicatorExpression(jsonNode.asText());
			}
		}
	}
	
	public void afterPageParse(IParseContext parseContext,Component component) {
		Tag tagComponent = (Tag) component;
		 DataSourceConfig dataSourceConfig=dataSourceUtil.getDataSourceConfigById(parseContext.getPage(), tagComponent.getDataSource());
		 if(dataSourceConfig==null) {
			 return;
		 }
		 Table table= tableStore.get(dataSourceConfig.getTableName());
		 Column column=table.getColumnByColumnName(tagComponent.getDataField());
		 
		 if(column!=null) { 
			 Expression visibleExpression=tagComponent.getVisibleExpression();
			 if(visibleExpression!=null) {
				 parseExpression(parseContext, component, visibleExpression, "visible", true);
			 }
			 
			 LookupInfo lookupInfo=column.getLookupInfo();
			 if(lookupInfo!=null) {
				 LookupInfo newLookupInfo=lookupInfo.clone();
				 tagComponent.setLookupInfo(newLookupInfo);
				 
				 DictDef dictDef=newLookupInfo.getDictDef();
				 
				 
				 String colorIndicatorExpression=tagComponent.getColorIndicatorExpression();
				 if(StringUtils.isEmpty(colorIndicatorExpression) && dictDef!=null) {
					 IrDict irDict=irDictService.getIrDictByCode(dictDef.getRefCode());
					 List<IrDictLine> dictLineList= irDictLineService.getDictLineListByDictId(irDict.getId());
					 colorIndicatorExpression="{";
					 for(int i=0;i<dictLineList.size();i++) {
						 if(i>0) {
							 colorIndicatorExpression+=","; 
						 }
						 IrDictLine dictLine=dictLineList.get(i);
						 colorIndicatorExpression+="\"";
						 colorIndicatorExpression+=dictLine.getCode();
						 colorIndicatorExpression+="\":\"";
						 colorIndicatorExpression+=dictLine.getColorValue();
						 colorIndicatorExpression+="\"";
					 }
					 colorIndicatorExpression+="}";
					 tagComponent.setColorIndicatorExpression(colorIndicatorExpression);
				 }
				 
				 DataSourceConfig columnDataSourceConfig=newLookupInfo.getDataSourceConfig();
				 if(columnDataSourceConfig!=null) {
					 String daynamicWhereClause=columnDataSourceConfig.getDynamicWhereClause();
					 if(daynamicWhereClause!=null) {
						 Expression daynamicWhereClauseExpression=ExpressionUtil.convertToExpression(daynamicWhereClause, Expression.EXPRESSION_TYPE_JAVASCRIPT);
						 newLookupInfo.setDynamicWhereClauseExpression(daynamicWhereClauseExpression);
						 parseExpression(parseContext, component, daynamicWhereClauseExpression, "where", true);
					 }
				 }
			 }
		 }
		 super.afterPageParse(parseContext, component);
	}
	
	@Override
	protected Component createComponent(IParseContext parseContext, ObjectNode objectNode) {
		// TODO Auto-generated method stub
		return new Tag();
	}

}
